package com.itboye.ihomebank.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.OpenIdBean;
import com.itboye.ihomebank.bean.PersonDataBean;
import com.itboye.ihomebank.bean.RegisterBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.KeyPresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.SavePersonData;
import com.itboye.ihomebank.util.controller.LoginController;
import com.itboye.ihomebank.util.controller.LoginedState;

import java.util.Observable;

/**
 * Created by Administrator on 2018/3/26 0026.
 */

public class RegisterActivityNew extends BaseOtherActivity {
    private EditText etPhone,verifyCode,loginPassword,confirmPassword;
    private TextView sendCode,registerOk;

    private UserPresenter userPresenter;
    //title
    private View v_statusbar;
    private ImageView close_icon;
    private TextView add_lift_tv,add_shap_title_tv;

    @Override
    protected int layout_id() {
        return R.layout.activity_register_new;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
      setStatusBarHeight(v_statusbar);
      add_shap_title_tv.setText("注册");
      userPresenter = new UserPresenter(this);

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.close_icon:
                this.finish();
                break;
            case R.id.sendCode://发送验证码
                if ("".equals(etPhone.getText().toString())){
                    ByAlert.alert("手机号不能为空");
                }else {
                   getyzm();
                }
                break;

            case R.id.registerOk: //注册
               VerificationYZM();
                break;
        }
    }

    /**
     * 获取验证码
     */
    private void getyzm() {
        userPresenter = new UserPresenter(this);
        showProgressDialog("请求中,轻稍后...",true);
        userPresenter.send(etPhone.getText().toString(), "1");
    }

    /**
     * 验证验证码是否正确
     */
    private void VerificationYZM() {
        if (verifyCode.getText().toString().trim().equals("")) {
            ByAlert.alert("请输入验证码");
            return;
        }
        userPresenter.verification(etPhone.getText().toString(), "1", verifyCode.getText().toString());
    }
    /**
     * 密码和确认密码判断并注册
     */
   private void toRegister() {
       if ("".equals(loginPassword.getText()) && loginPassword.getText().toString() == null) {
           ByAlert.alert("登录密码不能为空");
       } else if (!loginPassword.getText().toString().equals(confirmPassword.getText().toString())) {
           ByAlert.alert("两次输入内容不同，请重新输入");
       } else {
           userPresenter.register("3", "0", verifyCode.getText().toString(), etPhone.getText().toString(), loginPassword.getText()
                   .toString());
       }

   }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity=handlerError(data);
        if(entity!=null){
            if(!entity.getCode().equals(0)){
                if(entity.getEventType()==UserPresenter.send_success){
                    String bean=(String) entity.getData();
                    if(bean!=null){
                        ByAlert.alert("验证码发送成功");
                    }else{
                        ByAlert.alert("失败");
                    }
                }else if(entity.getEventType() == UserPresenter.send_fail){
                    ByAlert.alert(entity.getMsg());
                } else  if (entity.getEventType() == UserPresenter.verification_success) {
                    String bean = (String) entity.getData();
                    ByAlert.alert(bean);
                    toRegister();
                }else if (entity.getEventType() == UserPresenter.verification_fail) {
                    ByAlert.alert(entity.getMsg());
                }else if (entity.getEventType() == UserPresenter.register_success) {
                    RegisterBean bean = (RegisterBean) entity.getData();
                    Log.i("result", bean.toString());
                    if (bean != null) {
                        SPUtils.put(this, null,
                                SPContants.IS_LOGINED, true);
                        login();
                        this.finish();
                        // 发送广播通知LoginActivity结束
                        Intent intent1 = new Intent(LoginActivity.LOGINEXIT);
                        sendBroadcast(intent1);
                        LoginController.setLoginState(new LoginedState());
                        ByAlert.alert("注册成功");

                    }
                }else if (entity.getEventType() == UserPresenter.register_fail) {
                    ByAlert.alert("注册失败");
                } else if (entity.getEventType() == UserPresenter.user_success) {
                    PersonDataBean bean = (PersonDataBean) entity.getData();
                    if (bean != null) {
                        SavePersonData.saveData(this,bean);
                        userPresenter.bindKeJiXia(bean.getUid(),true);//直接注册科技侠
//						Intent intent1 = new Intent(ActivitySetting.EXITCHANGE);
//						sendBroadcast(intent1);
                        userPresenter.getOpenId(bean.getUid());//获取科技侠openid
                        // 发送广播通知更改头像
                        Intent intent2 = new Intent("changeavart");
                        sendBroadcast(intent2);
                    }
                }else if (entity.getEventType() == UserPresenter.user_fail){
                    ByAlert.alert(entity.getMsg());
                    startActivity(new Intent(this,LoginActivity.class));
                    this.finish();
                }else if (entity.getEventType() == UserPresenter.bindKeJiXia_success) {
                    //ByAlert.alert(entity.getData());
                } else if (entity.getEventType() == UserPresenter.bindKeJiXia_fail) {
                    //ByAlert.alert(entity.getData());
                }else if (entity.getEventType() == UserPresenter.openid_success) {
                    //ByAlert.alert(entity.getData());
                    OpenIdBean b= (OpenIdBean) entity.getData();
                    SPUtils.put(this, null, SPContants.OPENID, b.getOpenid());
                } else if (entity.getEventType() == UserPresenter.openid_fail) {
                    //ByAlert.alert(entity.getData());
                }
            }
        }
        try {
            closeProgressDialog();
        }catch (Exception e){

        }

    }

    /**
     * 登录
     */
    public void login() {
        userPresenter.login(etPhone.getText().toString(), loginPassword.getText().toString());
    }

}
